package com.share.servlet;

import com.share.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doPost(req,resp);
    }

    /*
    零、知识点
    1、servlet也是一个路径，同index.jsp一样。


    一、路径问题
    1、部署应用时：Application context=项目名如：sunxj-servlet
    2、path = req.getContextPath()======>/sunxj-servlet
    3、项目的根目录：basepath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"/";
        ======>http://localhost:8080/sunxj-servlet/
    总结：项目名=req.getContextPath()=Application context=sunxj-servlet=

    二、转发与重定向
    背景：
        Application context="/"
        form表单中action=”servlet/RegServlet“
    表单正常提交时:
        http://localhost:8080/
        http://localhost:8080/servlet/RegServlet
    req.getRequestDispatcher("/index.jsp").forward(req,resp); 转发时:
        http://localhost:8080/servlet/RegServlet
    resp.sendRedirect(req.getContextPath()+"/index.jsp"); 重定向时:
        http://localhost:8080/index.jsp
    总结：转发（服务器内部跳转）时，"/"  等价于  "/sunxj-servlet"


    三、当Application context= /sunxj-servlet时间

    1、http://localhost:8080/sunxj-servlet/reg.jsp
    2、http://localhost:8080/sunxj-servlet/servlet/RegServlet
    3、准发：req.getRequestDispatcher("/index.jsp").forward(req,resp); 中的"/"代表“/sunxj-servlet”，转发到http://localhost:8080/sunxj-servlet/index.jsp，但url不变
       重定向：resp.sendRedirect("/index.jsp");中的“/"代表”/“,重定向到http://localhost:8080/index.jsp，url会变

    注：form表单中的action相当于“重定向”，url会变，且"/"相当于"/"
       目前只有重定向中的"/"相当于"/"，转发或servlet-mapping、mvc-mapping中的"/"相当于应用名（Application Context）
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            User user = new User();
            String username, mypassword, gender;

            req.setCharacterEncoding("UTF-8");
            username = req.getParameter("username");
            mypassword = req.getParameter("mypassword");
            gender = req.getParameter("gender");
            System.out.println(username+mypassword+gender);

            user.setUsername(username);
            user.setMypassword(mypassword);
            user.setGender(gender);

            System.out.println("1"+req.getContextPath());
            System.out.println("2"+req.getServletPath());
            System.out.println("3"+req.getRequestURL());
            System.out.println("4"+req.getRequestURI());
            System.out.println("5"+req.getQueryString());
            System.out.println("6"+req.getPathInfo());


            req.getSession().setAttribute("user",user);
            //转发(服务器内部跳转)
            //"/":代表项目的根目录===>
//            req.getRequestDispatcher("/index.jsp").forward(req,resp);
            //不对
//            req.getRequestDispatcher(req.getContextPath()+"/index.jsp").forward(req,resp);
            //重定向
            //"index.jsp"时，代表当前路径下的index.jsp，当前路径是sunxj-servlet/servlet/
            resp.sendRedirect(req.getContextPath()+"/index.jsp");
//            resp.sendRedirect(req.getContextPath()+"/servlet/HelloServlet");
//            resp.sendRedirect("../index.jsp");
            //不对
//            resp.sendRedirect("/index.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
